package testJdk.commonUse.collections.list;

import com.google.common.collect.Lists;
import testJdk.anno.older.Person;
import org.junit.Test;

import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * @version 1.0.0
 * Created by cyd on 2022-06-09 09:32:14
 * @Description : 用于测试list的转型
 */
public class TranslateTest {

    /**
     * 这个案例说明，如果一个ArrayList向上转型为List
     * 那么它就可以强转回List
     */
    @Test
    public void translateListToArrayList() {
        // 简单类型可以转
        ArrayList<String> arrayList = Lists.newArrayList("1", "2", "3");
        arrayList = (ArrayList<String>) arrayList.stream().filter(e -> e.equals("1")).collect(Collectors.toList());
        arrayList.forEach(System.out::println);

        // 对象类型可以转
        Person milk = new Person("milk", 10);
        Person jack = new Person("jack", 15);
        ArrayList<Person> personArrayList = Lists.newArrayList(milk, jack);
        personArrayList = (ArrayList<Person>) personArrayList.stream().filter(e -> e.getAge() == 10).collect(Collectors.toList());
        personArrayList.forEach(person -> {
            System.out.println(person.toString());
        });
    }
}
